<?php
namespace app\admin\controller;

use app\admin\model\NewsModel;
use app\api\service\UAService;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

class Index extends Base
{
    public function index (Request $request)
    {
        $redis = new Redis();
        $newsModel = new NewsModel();
        $timeToday = strtotime ('today');
        $newsList = $newsModel->getList (['type' => ['in', '1,2']], $request->get ('page'), 5);
        $baseInfo = $redis->get ('ADMIN_BASE_INFO');
        if (empty($baseInfo)) {
            $baseInfo['visit_times'] = 0;
            $baseInfo['visit_times'] = Db::table ('tps_visited')->field ('id,user_agent,operation,browser,add_time')->where (['add_time' => ['between', [$timeToday - 86400, $timeToday]]])->count ('id');
            $baseInfo['food_num'] = Db::table ('tps_product')->where (['type' => ['egt', 3]])->count ();
            $redis->set ('ADMIN_BASE_INFO', $baseInfo, 60);
        }
        $browserList = Db::table ('tps_visited')->field ('browser,count(id) AS visit_times')->group ('browser')->order('visit_times DESC')->limit('6')->select ();
        return view ('index', [
            'newsList'    => $newsList,
            'browserList' => $browserList,
            'baseInfo'    => $baseInfo
        ]);
    }
}
